package com.choosefine.springboot.devops.base.config;

import com.choosefine.springboot.devops.participate.model.Participate;
import com.choosefine.springboot.devops.participate.model.RedisParticipate;

import java.math.BigDecimal;

/**
 * Created by Administrator on 2017/2/22 0022.
 */
public class BargainUtil {

    public static double bargain(RedisParticipate param){
        int sum=param.getBargainNumber();//砍价剩余次数.
        double max=param.getBargainMax();//最大砍价
        double min=param.getBargainMin();//最小砍价
        double button=param.getButtonPrice();//底价
        double price=param.getPrice();//当前价格
            double cha;
            if(sum==1){
                cha=price-button;
            }else{
                cha=min+(Math.random()*(max-min+0.001));
            }
            double quan=(price-button-cha)/(sum-1);//减少后均权
            if(quan>max){//权大于最大值时
                double nowQuan=(price-button)/(sum);//计算为减少时均权
                cha=nowQuan+(Math.random()*(max-nowQuan+0.001));//以权为底数。最大值与权的差做乘数
            }
            if(quan<min){//权小于最小值时
                double nowQuan=(price-button)/(sum-1);
                cha=min+(Math.random()*(quan-min+1));
            }
            BigDecimal c=new BigDecimal(cha);
        return c.setScale(2,BigDecimal.ROUND_HALF_UP).doubleValue();
    }
}
